//
//  UIViewController+QWAdd.m
//  Pods
//
//  Created by ZhangYanhua on 16/1/4.
//
//

#import "UIViewController+QWAdd.h"
#import <objc/runtime.h> 

static void *StoredNavBarInfoAssociatedObjectKey = &StoredNavBarInfoAssociatedObjectKey;

@implementation UIViewController (QWAdd)

@dynamic storedNavBarInfo;

- (void)setStoredNavBarInfo:(NSMutableDictionary *)storedNavBarInfo
{
    objc_setAssociatedObject(self, StoredNavBarInfoAssociatedObjectKey, storedNavBarInfo, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSMutableDictionary *)storedNavBarInfo
{
    return objc_getAssociatedObject(self, StoredNavBarInfoAssociatedObjectKey);
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view
{
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

- (void)storeCurrentNavigationBarStyle
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    [dict setValue:self.navigationController forKey:@"storedNavCtrl"];
    
    if ([self.navigationController.navigationBar respondsToSelector:@selector(barTintColor)]) {
        [dict setValue:self.navigationController.navigationBar.barTintColor forKey:@"storedBarTintColor"];
    }
    
    if ([self.navigationController.navigationBar respondsToSelector:@selector(tintColor)]) {
        [dict setValue:self.navigationController.navigationBar.tintColor forKey:@"storedTintColor"];
    }
    
    [dict setValue:[self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault] forKey:@"storedBarBgImage"];

    [dict setValue:@(self.navigationController.navigationBar.isTranslucent) forKey:@"storedIsTranslucent"];
    [dict setValue:self.navigationController.navigationBar.titleTextAttributes forKey:@"storedBarTitleAttrs"];
    
    [dict setValue:@(self.navigationController.isNavigationBarHidden) forKey:@"storedNavBarHidden"];
    [dict setValue:self.navigationController.navigationBar.backgroundColor forKey:@"storedBarBgColor"];
    [dict setValue:self.navigationController.navigationBar.shadowImage forKey:@"storedShadowImg"];

    self.storedNavBarInfo = dict;
}

- (void)restoreNavigationBarStyle
{
    NSDictionary *storedDict = self.storedNavBarInfo;

    UINavigationController *storedNavCtrl = storedDict[@"storedNavCtrl"];

    if ([storedNavCtrl.navigationBar respondsToSelector:@selector(barTintColor)]) {
        storedNavCtrl.navigationBar.barTintColor = storedDict[@"storedBarTintColor"];
    }

    if ([storedNavCtrl.navigationBar respondsToSelector:@selector(tintColor)]) {
        storedNavCtrl.navigationBar.tintColor = storedDict[@"storedTintColor"];
    }
    
    [storedNavCtrl.navigationBar setBackgroundImage:storedDict[@"storedBarBgImage"] forBarMetrics:UIBarMetricsDefault];
    
    [storedNavCtrl.navigationBar setTranslucent:[storedDict[@"storedIsTranslucent"] boolValue]];
    [storedNavCtrl.navigationBar setTitleTextAttributes:storedDict[@"storedBarTitleAttrs"]];
    
    [storedNavCtrl setNavigationBarHidden:[storedDict[@"storedNavBarHidden"] boolValue] animated:NO];
    
    [storedNavCtrl.navigationBar setBackgroundColor:storedDict[@"storedBarBgColor"]];
    
    [storedNavCtrl.navigationBar setShadowImage:storedDict[@"storedShadowImg"]];
}

@end
